import { Component, OnInit, ViewChild } from '@angular/core';
import { SchoolDTO } from 'src/app/benshuo/model/school-dto';
import { SchoolService } from 'src/app/benshuo/services/school.service';
import { MessagesDetailComponent } from '../../messages-detail/messages-detail.component';
import { MessagesService } from 'src/app/benshuo/services/messages.service';

@Component({
  selector: 'app-school-show',
  templateUrl: './school-show.component.html',
  styleUrls: [],
})
export class SchoolShowComponent implements OnInit {
  /**
   * 表格数据
   */
  tableData: Array<SchoolDTO>;

  /**
   * 调用api
   */
  spinning = false;

  /**
   * 企业名称
   */
  name = '';
  schooldiD: string;

  /**
   * 企业下拉
   */
  listData = [];
  /**
   * 提交申请组件
   */
  @ViewChild('messagesDetailComponent', null)
  messagesDetailComponent: MessagesDetailComponent;

  constructor(private service: SchoolService, private messages: MessagesService) {}

  ngOnInit() {
    this.load();
  }

  load(): void {
    this.spinning = true;
    this.service.getAll(this.schooldiD).subscribe(
      data => {
        this.tableData = data;
      },
      () => {
        this.afterApi();
      },
      () => {
        this.afterApi();
      },
    );
  }

  /**
   * 搜索
   */
  serach() {
    setTimeout(() => {
      this.load();
    }, 1);
  }

  checkedEvent($event: any) {
    // console.log($event);
  }
  afterApi() {
    this.spinning = false;
  }

  /**
   * 重置
   */
  reset() {
    this.serach();
  }

  notify($event) {
    this.load();
  }

  shenqing($event: SchoolDTO) {
    this.messagesDetailComponent.edit(null, $event);
  }

  updateShenqing($event: SchoolDTO) {
    this.messages.findById($event.messagesDTOS[0].id).subscribe(data => {
      this.messagesDetailComponent.edit(data, $event);
    });
  }
}
